package com.tonghe.service.impl;

import com.tonghe.domain.ResponseResult;
import com.tonghe.enums.AppHttpCodeEnum;
import com.tonghe.service.MailService;
import com.tonghe.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

/**
 * @Date:2022/8/15- 08- 15- 19:49
 * @author: tonghe
 **/
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Override
    public ResponseResult sendMail(String to, String content) {

        String from = "2996139769@qq.com";
        String subject = "村球地系河银";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content +"\n");

        try {
            javaMailSender.send(message);
        } catch (MailException e) {
            // 发送邮件事件失败！
            e.printStackTrace();
            return new ResponseResult().error(501,"邮箱发送失败，请检查邮箱格式");
        }
        // 成功发送邮件
        return new ResponseResult().ok(AppHttpCodeEnum.SEND_MAIL_SUCCESS);
    }
}
